﻿using DotNetty.Codecs.Http;
using DotNetty.Codecs.Http.WebSockets;
using DotNetty.Handlers.Timeout;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp1.dotnetty
{
    internal class WebSocketDotNettyInitializer : ChannelInitializer<ISocketChannel>
    {
        protected override void InitChannel(ISocketChannel channel)
        {
            //工作线程连接器 是设置了一个管道，服务端主线程所有接收到的信息都会通过这个管道一层层往下传输
            //同时所有出栈的消息 也要这个管道的所有处理器进行一步步处理
            IChannelPipeline pipeline = channel.Pipeline;

            //IdleStateHandler 心跳
            pipeline.AddLast(new IdleStateHandler(150, 0, 0));//第一个参数为读，第二个为写，第三个为读写全部

            pipeline.AddLast(new HttpServerCodec());
            pipeline.AddLast(new HttpObjectAggregator(65536));
            pipeline.AddLast(new WebSocketServerProtocolHandler("/ws"));
            pipeline.AddLast("WebSocketServer", new WebSocketDotNettyHandler());
        }
    }
}
